package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.OwnerActivity;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.mapper.OwnerActivityEnrollMapper;
import com.ruoyi.community.mapper.OwnerActivityMapper;
import com.ruoyi.community.service.IOwnerActivityEnrollService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.domain.OwnerActivityEnroll;

/**
 * 活动报名详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@Service
public class OwnerActivityEnrollServiceImpl implements IOwnerActivityEnrollService
{
    @Autowired
    private OwnerActivityEnrollMapper ownerActivityEnrollMapper;

    @Autowired
    private OwnerActivityMapper ownerActivityMapper;

//    private ISysUserService sysUserService;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询活动报名详情
     * 
     * @param id 活动报名详情ID
     * @return 活动报名详情
     */
    @Override
    public OwnerActivityEnroll selectOwnerActivityEnrollById(String id)
    {
        return ownerActivityEnrollMapper.selectOwnerActivityEnrollById(id);
    }

    /**
     * 查询活动报名详情列表
     * 
     * @param ownerActivityEnroll 活动报名详情
     * @return 活动报名详情
     */
    @Override
    public List<OwnerActivityEnroll> selectOwnerActivityEnrollList(OwnerActivityEnroll ownerActivityEnroll)
    {
        return ownerActivityEnrollMapper.selectOwnerActivityEnrollList(ownerActivityEnroll);
    }

    @Override
    public List<SysUser> selectOwnerActivityEnrollListById(String id)
    {
        List<OwnerActivityEnroll> listEnroll= ownerActivityEnrollMapper.selectOwnerActivityEnrollListById(id);
        List<SysUser> listUser = new ArrayList<>();
        SysUser query = new SysUser();
        for (OwnerActivityEnroll ownerActivityEnroll : listEnroll) {
//            SysUser enrollUser = sysUserMapper.selectUserById(1L, 1);
            query.setUserId(ownerActivityEnroll.getUserId());
            query.setCommunityId(SecurityUtils.getCommunityId());
            List<SysUser> enrollUsers= sysUserMapper.selectUserOnlyOnUser(query);
            listUser.addAll(enrollUsers);
        }
        return listUser;
    }

    /**
     * 新增活动报名详情
     * 
     * @param ownerActivityEnroll 活动报名详情
     * @return 结果
     */
    @Override
    public int insertOwnerActivityEnroll(OwnerActivityEnroll ownerActivityEnroll)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ownerActivityEnroll.setUserId(loginUser.getUserId());
        int hasEnrolled = ownerActivityEnrollMapper.hasEnrolled(ownerActivityEnroll.getUserId().toString(), ownerActivityEnroll.getActivityId());
        if(hasEnrolled==1){
            return -1;
        }else{
            OwnerActivity ownerActivity = ownerActivityMapper.selectOwnerActivityById(ownerActivityEnroll.getActivityId());
            Long enrollNumber = ownerActivity.getEnrollNumber() + 1;
            ownerActivity.setEnrollNumber(enrollNumber);
            ownerActivityMapper.updateOwnerActivity(ownerActivity);
            return ownerActivityEnrollMapper.insertOwnerActivityEnroll(ownerActivityEnroll);
        }
    }

    public boolean hasEnrolled(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasEnrolled = ownerActivityEnrollMapper.hasEnrolled(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        return hasEnrolled==1;
    }

    /**
     * 修改活动报名详情
     * 
     * @param ownerActivityEnroll 活动报名详情
     * @return 结果
     */
    @Override
    public int updateOwnerActivityEnroll(OwnerActivityEnroll ownerActivityEnroll)
    {
        return ownerActivityEnrollMapper.updateOwnerActivityEnroll(ownerActivityEnroll);
    }

    /**
     * 批量删除活动报名详情
     * 
     * @param ids 需要删除的活动报名详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityEnrollByIds(String[] ids)
    {
        return ownerActivityEnrollMapper.deleteOwnerActivityEnrollByIds(ids);
    }

    /**
     * 删除活动报名详情信息
     * 
     * @param id 活动报名详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityEnrollById(String id)
    {
        return ownerActivityEnrollMapper.deleteOwnerActivityEnrollById(id);
    }
}
